package com.bbzn.device.client.service.impl;

import com.bbzn.device.client.constant.QiniuApiConstant;
import com.bbzn.device.client.dto.api.qiniu.DelFileRequest;
import com.bbzn.device.client.service.QiniuOssApiService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.rs.RSClient;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.concurrent.Executor;

@ToString
@Slf4j
@Service
public class QiniuOssApiServiceImpl implements QiniuOssApiService {

    @Autowired
    public SystemParamByCacheService systemParamByCacheService;

    @Autowired
    @Qualifier("threadPoolExecutorForVideo")
    private Executor executor;

    public HashMap<String, String> params = new HashMap<>();

    public String accessKey;

    public String secretKey;

    @PostConstruct
    public void init()  {
        try {
            accessKey = systemParamByCacheService.getValue(QiniuApiConstant.QINIUAPI,QiniuApiConstant.ACCESSKEY).getValue();
            secretKey = systemParamByCacheService.getValue(QiniuApiConstant.QINIUAPI, QiniuApiConstant.SECRETKEY).getValue();
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    /**
     * 七牛云OSS文件删除接口
     *
     * @return
     * @throws Exception
     */
    @Override
    public void delOssFile(DelFileRequest request) throws Exception {
        if (StringUtils.isEmpty(request.getBucketName()) || StringUtils.isEmpty(request.getKey()) ) {
            throw new RuntimeException("基本參數為空");
        }
        Config.ACCESS_KEY = accessKey;
        Config.SECRET_KEY = secretKey;
        Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
        RSClient client = new RSClient(mac);
        client.delete(request.getBucketName(), request.getKey());
    }

}
